function openwindow(url)
{
	var options = "scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes";
	options += ',width=' + screen.availWidth + ',height=' + screen.availHeight;
	options += ',screenX=0,screenY=0,top=0,left=0';

	win = window.open(url, '', options);
	win.focus();
	win.moveTo(0, 0);
}


function isValidDate(dateStr, format)
{
   if (format == null){ format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) ||
      (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      // Tan :: Start : if 'd-m-Y' is invalid format (only remove -)
   	  var reg1 = /^\d{1,2}(\/)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
      // Tan :: End
   }

   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }

   return true;
}

//<!-- salimane start edit -->

	// Removes leading whitespaces
	function LTrim( value ){
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
	}
	// Removes ending whitespaces
	function RTrim( value ){
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
	}
	// Removes leading and ending whitespaces
	function trim( value ){
		return LTrim(RTrim(value));
	}
	function textValid(e){
		if (trim(e.value) == "" || e.value.length == 0)
			return false;
		else
			return true;
	}

//<!-- salimane end edit -->

//convert a string into a Date object
/**
 * convert a string into a Date object
 *
 * @param unknown $date
 * @return Date if $date was pass correctly format; false otherwise
 */
function strtodate(date)
{
	try{
		var dateArr = date.split("/");
		if (dateArr.length == 3)
		{
			var ret = new Date();
			ret.setFullYear(dateArr[2], dateArr[1]-1, dateArr[0]);
		}
		else {
			return ret = false;
		}
	}
	catch(err)
	{
		ret = false;
	}
	return ret;
}

function confirmDeletion()
{
	return confirm('Etes vous certain de vouloir supprimer cet objet ?');
}

function redirect(url)
{
	window.location.href = url;
}

function isEmpty(val)
{
	if( /^$/.test(val))
		return true;
	else
		return false
}

//<!--salimane start edit-->
function isEmptyTextBox(control, error_message)
{
	if (trim(control.value) == "" || control.value.length == 0)
	{
		if (error_message != null)
			showErrorBubble(control, error_message);
		return true;
	}

	return false;
}

/**
 * Check if the textbox value max length is valided
 *
 * @param object $control
 * @param int $strlength
 * @param string $error_message
 * @return true if $control.value length is not excceed $strlength; false otherwise
 */
function isValidLengthTextBox(control, strlength, error_message)
{
	if (control.value == null || control.value.length > strlength)
	{
		showErrorBubble(control, error_message);
		//control.focus();
		return false;
	}
	return true;
}

/**
 * Check if the textbox value min length is valided
 *
 * @param object $control
 * @param int $strlength
 * @param string $error_message
 * @return true if $control.value length is more $strlength long; false otherwise
 */
function isValidMinLengthTextBox(control, strlength, error_message)
{
	if (control.value == null || control.value.length < strlength)
	{
		showErrorBubble(control, error_message);
		return false;
	}
	return true;
}

function isInvalidDateTextBox(control, error_message)
{
	if( control.value.length > 0 && !isValidDate(trim(control.value), "DMY"))
	{
		showErrorBubble(control, error_message);
		//control.focus();
		return true;
	}

	return false;
}

function isInvalidHourTextBox(control, error_message)
{
	if( control.value.length > 0 && !isValidHour(trim(control.value)))
	{
		showErrorBubble(control, error_message);
		//control.focus();
		return true;
	}

	return false;
}
function isValidHour(hourStr)
{
	var reg1 = /^\d{1,2}(\:)\d{1,2}$/
	return reg1.test(hourStr);
}


//<!--salimane end edit-->


function isInvalidIntTextBox(control, error_message)
{
	if(isNaN(control.value))
	{
		showErrorBubble(control, error_message);
		return true;
	}
	return false;
}

function isInvalidFloatTextBox(control, error_message)
{
	if(isNaN(control.value))
	{
		showErrorBubble(control, error_message);
		return true;
	}

	return false;
}
 //show tooltip when input data is not valid
 function showErrorBubble(control, error_msg, seconds)
 {
	if( isNaN(seconds))
	{
		seconds = 5;
	}

	if(MSIE6)
	{
		bu = $("myIfrm");
		if(!bu)
		{
			iframeObj = document.createElement('IFRAME');
			iframeObj.style.position = 'absolute';
			iframeObj.border='0px';
			iframeObj.style.border = '0px';
			iframeObj.src = 'javascript:false';
			iframeObj.scrolling = 'no';
			iframeObj.id = 'myIfrm';
			iframeObj.style.filter = 'Alpha(opacity=0)';
			document.body.appendChild(iframeObj);
		}
	}

	ba = $("bubble_tooltip");
	if(!ba)
	{
		tooltipDiv = document.createElement('div');
		tooltipDiv.id = 'bubble_tooltip';
		tooltipDiv.innerHTML = "<div class='bubble_top'></div>"
						+"<div class='bubble_middle'><span id='bubble_tooltip_content'></span></div>"
						+"<div class='bubble_bottom'></div>";
		tooltipDiv.style.zIndex = 1000;
		document.body.appendChild(tooltipDiv);
	}

	showToolTip(control,'<img class="small" src="' + applicationUrl + 'js/bubble-tooltip/images/info2_small.png" border="0"/><div align="center">' + error_msg + '</div>',
	'bubble_tooltip','bubble_tooltip_content',seconds);

	control.focus();
 }

 function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);

	cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents + ' &#x80');
}

function emailCheck(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid
	if (user.match(userPat)==null) {
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			return false
		    }
	    }
	    return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length

	if (domArr[domArr.length-1].length<2 ||
	    domArr[domArr.length-1].length>3) {
	   return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

/*
function isValidEmail(str)
{
	apos=str.indexOf("@");
	dotpos=str.lastIndexOf(".");

	if (apos<1||dotpos-apos<2)
  		return false;
	else
		return true;
}
*/

/**
 * Check if the textbox email value is valided. That is the email that has:
 * Domain name rule: - Use only letters, numbers, or hyphen ('-')
 * 					 - Can not begin or end with a hyphen
 * 					 - Must have less than 63 characters, not include extension
 *
 * @param object $control
 * @param string $error_message
 * @return true if $control.value is a valid email; false otherwise
 */
function isValidEmail(control, error_message)
{
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	//var reg = /^(.+)[@]((\.)([^-~!@#$%^&*()_+={\[\]}:;"'<,>.?\/\|\\][-a-z0-9]{0,62}[^-~!@#$%^&*()_+={\[\]}:;"'<,>.?\/\|\\]))+?\.([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/;
	var subj = trim(control.value);
	subj = subj.replace(/(\"|\')/gi, "");
	if(!subj.match(reg))
	{
		showErrorBubble(control, error_message);
		//control.focus();
		return false;
	}
	return true;
}


/*
* [anhtuan@dirox] check whether or not current value is a positive whole integer
*/
function isPositiveWholeInteger(myValue){
	var ValidChars = "0123456789";
	
	var Char;

	if(isNaN(parseInt(myValue)) || parseInt(myValue) < 0){
		return false;
	}else{
		for (m = 0; m < myValue.length; m++)
		{
			Char = myValue.charAt(m);
			if (ValidChars.indexOf(Char) == -1)
			{
				return false;
			}
		}
	}

	return true;
}


/*
* [anhtuan@dirox] convert Date object to a string with format of Mysql Date
*/
function convertDateObjectToMysqlDateString(myObjDate){
	var temp_full_date = myObjDate.getFullYear();

	var temp_month = myObjDate.getMonth() + 1;

	var temp_date = myObjDate.getDate();

	if (temp_month < 10)
	{
		temp_month = "0" + temp_month;
	}

	if (temp_date < 10)
	{
		temp_date = "0" + temp_date;
	}

	temp_full_date = temp_full_date + "-" + temp_month + "-" + temp_date;

	return temp_full_date;
}


function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}

	return IsNumber;
}

/**
 *	Define array method if Not Compatibility Array filter, every method.
 */
 if (!Array.prototype.filter)
	{
	  Array.prototype.filter = function(fun /*, thisp*/)
	  {
		var len = this.length;
		if (typeof fun != "function")
		  throw new TypeError();

		var res = new Array();
		var thisp = arguments[1];
		for (var i = 0; i < len; i++)
		{
		  if (i in this)
		  {
			var val = this[i]; // in case fun mutates this
			if (fun.call(thisp, val, i, this))
			  res.push(val);
		  }
		}

		return res;
	  };
	}

 if (!Array.prototype.every)
	{
	  Array.prototype.every = function(fun /*, thisp*/)
	  {
		var len = this.length;
		if (typeof fun != "function")
		  throw new TypeError();

		var thisp = arguments[1];
		for (var i = 0; i < len; i++)
		{
		  if (i in this &&
			  !fun.call(thisp, this[i], i, this))
			return false;
		}

		return true;
	  };
	}


/**
* Convert text to UTF-8
*/
function text_convert_to_utf8(strValue){
	var moji = strValue;
	var moji_x = "";

	moji_length = moji.length;

	for(var i=0;i<moji_length;i++){
		moji_x = moji_x + "&" + "#" + moji.charCodeAt(i) + ";";
	}

	return moji_x;
}

function URLEncode (clearString) {
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
			x += match[1].length;
		} else {
			if (clearString[x] == ' ')
				output += '+';
			else {
				var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
			}
			x++;
		}
	}
	output = output.replace(/%2F/g, '%252F');
	output = output.replace(/%5C/g, '%255C');

	return output;
}

function isUrl(url){
	url = url.toLowerCase();
	return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}